package databasetest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Update extends JInternalFrame {
	private JLabel lblNewLabel;
	private JLabel lblEa;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	Connection conn;
	private JLabel lblSerial;
	private JTextField serial;
	Vector<String> vex;
	DatabaseTest dt;

	/**
	 * Create the frame.
	 * @wbp.parser.constructor
	 */
	public Update(Connection c,DatabaseTest d) {
		super("수정",true,true,true,false);
		setBounds(100, 100, 450, 177);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblEa());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getLblSerial());
		getContentPane().add(getSerial());
		this.conn = c;
		this.dt = d;
//	
//		serial.setText(vex.get(0));
//		code.setText(vex.get(1));
//		ea.setText(vex.get(2));
////		
	}

	public Update(Connection conn2, Vector<String> vx) {
		super("수정",true,true,true,false);
		setBounds(100, 100, 450, 177);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblEa());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getLblSerial());
		getContentPane().add(getSerial());
		
	
		serial.setText(vex.get(0)+"");
		code.setText(vex.get(1)+"");
		ea.setText(vex.get(2)+"");
		this.conn = conn2;
		this.vex = vx;
	}
	
	public void setData(String s,String c,String e){
		serial.setText(s);
		code.setText(c);
		ea.setText(e);
		
	}
	
	
	
	public void setUpdate2(){
		int s = Integer.parseInt(serial.getText());
		String sql = "select * from ptbl where serial = ?";
		try{
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, s);
			
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				code.setText(rs.getString("code"));
				ea.setText(""+rs.getInt("ea"));
			}else{
				System.out.println("데이터가 없습니다.");
			}
			rs.close();
			pstmt.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("code");
			lblNewLabel.setBounds(10, 41, 57, 29);
		}
		return lblNewLabel;
	}
	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("ea");
			lblEa.setBounds(10, 80, 57, 29);
		}
		return lblEa;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(89, 45, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(89, 84, 116, 21);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					setUpdate2();
//					setData();
				}
			});
			btnNewButton.setBounds(217, 44, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("수정");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s = Integer.parseInt(serial.getText());
					String c = code.getText();
					int e = Integer.parseInt(ea.getText());
					String sql = "update ptbl set code=?, ea=?" +"where serial = ?";
					try{
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, e);
						pstmt.setInt(3, s);
						
						pstmt.executeUpdate();
						pstmt.close();
						if(dt.select != null)dt.select.setList();
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			btnNewButton_1.setBounds(89, 115, 97, 23);
		}
		return btnNewButton_1;
	}
	private JLabel getLblSerial() {
		if (lblSerial == null) {
			lblSerial = new JLabel("serial");
			lblSerial.setBounds(10, 6, 57, 29);
		}
		return lblSerial;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(89, 10, 116, 21);
		}
		return serial;
	}
}
